package cn.zz.camerasdk.filter.util;

import cn.zz.camerasdk.filter.Rotation;

/**
 * author: zhu on 2017/7/7 17:19
 * email: mackkill@gmail.com
 */

public class TextureRotationUtil {
    public static final float[] TEXTURE_NO_ROTATION = new float[]{0.0F, 1.0F, 1.0F, 1.0F, 0.0F, 0.0F, 1.0F, 0.0F};
    public static final float[] TEXTURE_ROTATED_90 = new float[]{1.0F, 1.0F, 1.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F};
    public static final float[] TEXTURE_ROTATED_180 = new float[]{1.0F, 0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 0.0F, 1.0F};
    public static final float[] TEXTURE_ROTATED_270 = new float[]{0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 0.0F, 1.0F, 1.0F};

    private TextureRotationUtil() {
    }

    public static float[] getRotation(Rotation rotation, boolean flipHorizontal, boolean flipVertical) {
        float[] rotatedTex;
        switch (rotation) {
            case ROTATION_90:
                rotatedTex = TEXTURE_ROTATED_90;
                break;
            case ROTATION_180:
                rotatedTex = TEXTURE_ROTATED_180;
                break;
            case ROTATION_270:
                rotatedTex = TEXTURE_ROTATED_270;
                break;
            case NORMAL:
            default:
                rotatedTex = TEXTURE_NO_ROTATION;
                break;
        }

        if (flipHorizontal) {
            rotatedTex = new float[]{flip(rotatedTex[0]), rotatedTex[1], flip(rotatedTex[2]), rotatedTex[3], flip(rotatedTex[4]), rotatedTex[5], flip(rotatedTex[6]), rotatedTex[7]};
        }

        if (flipVertical) {
            rotatedTex = new float[]{rotatedTex[0], flip(rotatedTex[1]), rotatedTex[2], flip(rotatedTex[3]), rotatedTex[4], flip(rotatedTex[5]), rotatedTex[6], flip(rotatedTex[7])};
        }

        return rotatedTex;
    }

    private static float flip(float i) {
        return i == 0.0F ? 1.0F : 0.0F;
    }
}
